﻿using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Estsh.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Estsh.Core.Controllers;
using Newtonsoft.Json;

namespace Estsh.Core.Web.Areas.Wms.Controllers.Qc
{
    [Route("wms/[controller]")]
    [ApiController]
    public class QcController : WmsBaseController
    {
        private IQcService service;

        // GET: LoginController
        public QcController(IQcService _service)
        {
            service = _service;
        }

        /// <summary>
        /// 获取质检单信息
        /// </summary>
        /// <returns>工厂代码1002</returns>
        [HttpPost("GetQCListing")]
        public IActionResult GetQCListing(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string cartonno = jobj["cartonno"].ToString();
                List<WmsQcDetail> obj = service.GetQCListing(cartonno );

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
        /// <summary>
        /// 订单质检情况
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("GetShowList")]
        public IActionResult GetShowList(Newtonsoft.Json.Linq.JObject jobj)
        {
            string receving = jobj["receving"].ToString();
            string loginId = jobj["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsQcShowList> obj = service.GetShowList(receving, loginId);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 获取质检结果
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("GetQcRusult")]
        public IActionResult GetQcRusult(Newtonsoft.Json.Linq.JObject jobj)
        {
            string receving = jobj["receving"].ToString();
            string loginId = jobj["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                int obj = service.GetQcRusult(receving, loginId);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 完成质检
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("CheckQc")]
        public IActionResult CheckQc(Newtonsoft.Json.Linq.JObject jobj)
        {
            string receving = jobj["receving"].ToString();
            string loginId = jobj["loginId"].ToString();
            string remark = jobj["remark"].ToString();
            string qcresult= jobj["QcResult"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckQc(receving,loginId,remark, qcresult);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 扫描不良数进行拆分
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("CheckNcQty")]
        public IActionResult CheckNcQty(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            string loginId = jobj["loginId"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            string ncQty = jobj["ncQty"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckNcQty(cartonNo, loginId, orderNo, ncQty);

                result.Success = true;

                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }


        [HttpPost("CheckCartonStatus")]
        public IActionResult CheckCartonStatus(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            string loginId = jobj["loginId"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckCartonStatus(cartonNo, loginId, orderNo);

                result.Success = true;

                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
        [HttpPost("CheckQcCarton")]
        public IActionResult CheckQcCarton(Newtonsoft.Json.Linq.JObject jobj)
        {
            string QcCartonNo = jobj["QcCartonNo"].ToString();
            string MAXmessage = jobj["MAXmessage"].ToString();
            string loginId = jobj["loginId"].ToString();
            string Remark = jobj["Remark"].ToString();
            string receving = jobj["receving"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckQcCarton(QcCartonNo,MAXmessage,loginId,Remark,receving);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
    }

}
